package com.example.plugindemo;

import android.content.Intent;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;

public class IActivityManagerProxy implements InvocationHandler {

    private Object mActivityManager;

    public IActivityManagerProxy(Object mActivityManager){
        this.mActivityManager = mActivityManager;
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {

        if(method.getName().equals("startActivity")){
            int index =0;
            for(int i=0;i<args.length;i++){
                if(args[i] instanceof Intent){
                    index = i;
                    break;
                }
            }
            Intent intent = (Intent) args[index];
            Intent subIntent = new Intent();
            String packageName = "com.example.plugindemo";
            subIntent.setClassName(packageName,packageName+".StubActivity");
            subIntent.putExtra(HookHelper.TARGET_INTENT,intent);
            args[index] = subIntent;
        }
        return method.invoke(mActivityManager,args);
    }
}
